﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BasicFramework.Devices;
using BasicFramework.Helper;

namespace BasicFramework.Services
{
    public class ServiceManager
    {
        #region Fields

        private DeviceContextBase context;
        private List<IGameService> services;

        #endregion


        #region Properties

        #endregion


        #region Constructors

        public ServiceManager(DeviceContextBase deviceContext)
        {
            Logger.Info("Initialize Service Manager");
            this.context = deviceContext;
            new ServiceProvider(this);
            this.services = new List<IGameService>();
            Logger.Info("Service Manager initialized");

        }

        #endregion


        #region Events

        #endregion


        #region Methods

        public void AddService(IGameService service)
        {
            if (this.services.Contains(service))
            {
                // TODO: Exception Handling
                return;
            }

            this.services.Add(service);

        }

        public void RemoveService<T>() where T : class, IGameService
        {
            if (this.services.Exists(x => x is T))
            {
                // Remove Service
                this.services.Remove(this.services.Find(x => x is T));
            }
        }


        public T Find<T>() where T : class, IGameService
        {
            foreach (IGameService gameService in this.services)
            {
                if (gameService is T)
                {
                    return (T)gameService;
                }
            }
            return null;
        }

        #endregion

    }
}
